package cn.tedu.bs.admin.service.impl;

import cn.tedu.bs.admin.ex.ServiceException;
import cn.tedu.bs.admin.mapper.UserPayMonthlyMapper;
import cn.tedu.bs.admin.pojo.entity.UserPayMonthly;
import cn.tedu.bs.admin.pojo.param.UserPayMonthlyAddNewParam;
import cn.tedu.bs.admin.pojo.param.UserPayMonthlyUpdateInfoParam;
import cn.tedu.bs.admin.pojo.vo.PageData;
import cn.tedu.bs.admin.pojo.vo.UserPayMonthlyListItemVO;
import cn.tedu.bs.admin.pojo.vo.UserPayMonthlyStandardVO;
import cn.tedu.bs.admin.service.IUserPayMonthlyService;
import cn.tedu.bs.admin.util.PageInfoToPageDataConverter;
import cn.tedu.bs.admin.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.bs.admin.util.ServiceUtil.saveFileToStorage;

@Slf4j
@Service
public class UserPayMonthlyImpl implements IUserPayMonthlyService {
    @Autowired
    private UserPayMonthlyMapper userPayMonthlyMapper;

    @Override
    public void addNew(UserPayMonthlyAddNewParam userPayMonthlyAddNewParam) {
        log.debug("开始处理【添加用户月消费】的业务，参数：{}", userPayMonthlyAddNewParam);
// 将用户月消费数据写入到数据库中
        UserPayMonthly userPayMonthly = new UserPayMonthly();
        BeanUtils.copyProperties(userPayMonthlyAddNewParam, userPayMonthly);
        userPayMonthly.setGmtCreated(LocalDateTime.now());
        int rows = userPayMonthlyMapper.insert(userPayMonthly);
        if (rows != 1) {
            String message = "添加用户月消费失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的用户月消费数据插入到数据库，完成！");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除美容用户月消费】的业务，id：{}", id);
        userPayMonthlyMapper.deleteById(id);
        log.debug("将id为{}的美容用户月消费数据从数据库中删除，完成！", id);
    }

    @Override
    public void updateInfoById(Long id, UserPayMonthlyUpdateInfoParam userPayMonthlyUpdateInfoParam) {
        log.debug("开始处理【修改美容用户月消费详情】的业务，参数：{}", userPayMonthlyUpdateInfoParam);
        // 检查用户月消费是否存在，如果不存在，则抛出异常
        QueryWrapper<UserPayMonthly> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = userPayMonthlyMapper.selectCount(queryWrapper);
        log.debug("根据用户月消费ID统计匹配的用户月消费数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改用户月消费详情失败，用户月消费数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //修改用户月消费
        UserPayMonthly userPayMonthly = new UserPayMonthly();
        BeanUtils.copyProperties(userPayMonthlyUpdateInfoParam, userPayMonthly);
        userPayMonthly.setId(id);
        int rows = userPayMonthlyMapper.updateById(userPayMonthly);
        if (rows != 1) {
            String message = "修改用户月消费失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("将新的美容用户月消费数据更新入到数据库，完成！");
    }

    @Override
    public UserPayMonthlyStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询用户月消费详情】的业务，参数：{}", id);
        UserPayMonthlyStandardVO queryResult = userPayMonthlyMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询用户月消费详情失败，用户月消费数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<UserPayMonthlyListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<UserPayMonthlyListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询用户月消费列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<UserPayMonthlyListItemVO> list = userPayMonthlyMapper.list();
        PageInfo<UserPayMonthlyListItemVO> pageInfo = new PageInfo<>(list);
        PageData<UserPayMonthlyListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    //模糊查询姓名
    @Override
    public PageData<UserPayMonthlyListItemVO> getListByUserName(Integer pageNum, Integer pageSize, String userName) {
        log.debug("开始处理【根据用户名字查询用户月消费】的业务，参数：{}", userName);
        PageHelper.startPage(pageNum, pageSize);
        List<UserPayMonthlyListItemVO> queryResult = userPayMonthlyMapper.getListByUserName(userName);
        PageInfo<UserPayMonthlyListItemVO> pageInfo = new PageInfo<>(queryResult);
        PageData<UserPayMonthlyListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", queryResult);
        return pageData;
    }


}